# Maintainer: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
pkgname=openscad
pkgver=2021.01
pkgrel=14
pkgdesc="The programmers solid 3D CAD modeller"
url="http://openscad.org/"
# Glew and GLES on ARM don't play well with each other
# tests fails on s390x
# tests hangs on riscv64
arch="all !armv7 !aarch64 !armhf !s390x !riscv64"
license="GPL-2.0-only"
subpackages="$pkgname-doc"
makedepends="
	bison
	boost-dev
	cairo-dev
	cgal-dev
	cmake
	double-conversion-dev
	eigen-dev
	flex
	glew-dev
	gmp-dev
	harfbuzz-dev
	libxml2-dev
	libzip-dev
	libspnav-dev
	mpfr-dev
	opencsg-dev
	qscintilla-dev
	qt5-qtbase-dev
	qt5-qtmultimedia-dev
	"
checkdepends="
	imagemagick
	mesa
	mesa-dri-gallium
	python3
	xvfb-run
	"
source="
	https://files.openscad.org/openscad-$pkgver.src.tar.gz

	0001-fix-CVE-2022-0496.patch
	0002-fix-CVE-2022-0497.patch
	0003-fix-build-with-cgal-5.3.patch
	0004-fix-build-with-cgal-5.4.patch
	0005-fix-boost-join.patch
	"

# Handle tests failing only on some arch's, or building against libs only
# provided by some arch's.
case "$CARCH" in
	x86_64)
		makedepends="$makedepends lib3mf-dev"
		export LIB3MF_INCLUDEPATH="/usr/include/lib3mf/Bindings/Cpp"
		;;
	x86)
		makedepends="$makedepends lib3mf-dev"
		export LIB3MF_INCLUDEPATH="/usr/include/lib3mf/Bindings/Cpp"
		_arch_failing_tests="
			3mfpngtest_cube10
			"
		;;
	s390x)
		_arch_failing_tests="
			3mfpngtest_cube10
			dxfpngtest_polygon-intersect
			cgalbinstlcgalpngtest_bad-stl-pcbvicebar
			cgalbinstlcgalpngtest_bad-stl-tardis
			cgalbinstlcgalpngtest_fn_bug
			cgalbinstlcgalpngtest_issue904
			cgalbinstlcgalpngtest_issue1215c
			cgalbinstlcgalpngtest_issue1225
			cgalpngtest_import_3mf-tests
			cgalpngtest_intersection_for-tests
			cgalpngtest_issue1215b
			cgalpngtest_issue1803
			cgalpngtest_multiple-layers
			cgalpngtest_polygon-overlap
			csgpngtest_disable-modifier
			csgpngtest_import_3mf-tests
			csgpngtest_intersection-tests
			csgpngtest_module-recursion
			csgpngtest_projection-cut-tests
			csgpngtest_scale2D-tests
			csgpngtest_rotate_extrude-hole
			dxfpngtest_text-font-spacing-tests
			opencsgtest_import_3mf-tests
			svgpngtest_module_recursion
			throwntogethertest_import_3mf-tests
			throwntogethertest_issue3262
			"
		;;
	*)
		# lib3mf is not available on all other archs
		_arch_failing_tests="
			3mfpngtest_cube10
			cgalpngtest_import_3mf-tests
			csgpngtest_import_3mf-tests
			opencsgtest_import_3mf-tests
			throwntogethertest_import_3mf-tests
			"
		;;
esac

# secfixes:
#   2021.01-r2:
#     - CVE-2022-0496
#     - CVE-2022-0497

build() {
	qmake-qt5 PREFIX="/usr"
	make
}

check() {
	cd tests
	# collect failing tests in a way that creates friendly git diffs
	local failing_tests
	failing_tests="
		3mfexport_3mf-export
		astdumptest_allexpressions
		cgalbinstlcgalpngtest_polyhedron-nonplanar-tests
		cgalpngtest_nef3_broken
		cgalpngtest_polyhedron-nonplanar-tests
		cgalstlcgalpngtest_polyhedron-nonplanar-tests
		csgpngtest_nef3_broken
		csgpngtest_polyhedron-nonplanar-tests
		echotest_allexpressions
		echotest_function-literal-.*
		lazyunion-.*
		monotonepngtest_polyhedron-nonplanar-tests
		opencsgtest_nef3_broken
		pdfexporttest_centered
		pdfexporttest_simple-pdf
		throwntogethertest_nef3_broken
		$_arch_failing_tests
		"

	# trim whitespace before and after list and separate expressions by | (regex or)
	failing_tests="$(echo "$failing_tests" | tr -d '\n' | sed -e 's/^\s*//' -e 's/\s*$//' -e 's/\s\+/|/g')"

	cmake \
		-DEXPERIMENTAL=OFF \
		.
	make
	xvfb-run ctest -E "$failing_tests"
}

package() {
	make INSTALL_ROOT="$pkgdir" install
}

sha512sums="
8deaa26bf4c295c12da38f323d2b2e6f827851337f5bc1cc9c79afc083c9f913c19a263086e6e853bf2c8434c1ccc705ea22ddb02dc99d39bb1e5e03fc58d128  openscad-2021.01.src.tar.gz
369353f13bb6d2bf123af1e25b5f264cd757efa5d0cd8c226a3803a41a0fdb74aff7411ddf91cb394f9cd295e10e05159487fb6e9c2d661280bf060e391bc3da  0001-fix-CVE-2022-0496.patch
708a491c5a6166bdf92419bce68dbcb4eccbb97ce1880c5a3c919a90ae5af19aa9092e46b81fe8705e8470189d43c4ecad19dece9f1e405681a186c0bd8f5ca4  0002-fix-CVE-2022-0497.patch
090b8a8e53cc7560695d4065db891a3602923d893d752e3d007a42f2cd32c5f6f444d735d716658fb6f3d979e8cf2489cbff3a58a9070621ab5448a6dbf3372b  0003-fix-build-with-cgal-5.3.patch
7d3067bcadf5a56b3b21b80f36e8d1fd99047029b86db6803b71760d47a8b199d3485c6486767707273aa57f9a83aaafcb763fe7a1ee295b01a776f7e5db038f  0004-fix-build-with-cgal-5.4.patch
f0538eb98543a2a15534794f91c35f10d21fe04a8696639a4f08cd98daf63e6efc855b102f41ecb05a1e1bffeecc7c445fcfad7c3a90f32ec28c5a1646863466  0005-fix-boost-join.patch
"
